import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xmshop/app/models/category_model.dart';
import 'package:xmshop/app/serives/httpsClient.dart';
import '../../../models/focus_model.dart';
import '../../../models/plist_model.dart';

class HomeController extends GetxController {
  //TODO: Implement HomeController

  ScrollController scrollController = ScrollController();

  RxBool flag = false.obs;
  RxBool isMi = true.obs;

  RxList<FocusItem> swiperList = <FocusItem>[].obs;
  RxList<FocusItem> bestSellingSwiperList = <FocusItem>[].obs;
  RxList<PlistItemModel> sellingPlist = <PlistItemModel>[].obs;
  RxList<PlistItemModel> bestPlist = <PlistItemModel>[].obs;

  RxList<CategoryItem> CategoryList = <CategoryItem>[].obs;

  HttpsClient httpsClient = HttpsClient();

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(() {
      if (scrollController.position.pixels > 10) {
        if (flag.value == false) {
          isMi.value = false;
          flag.value = true;
          update();
        }
      }
      if (scrollController.position.pixels < 10) {
        if (flag.value == true) {
          flag.value = false;
          update();
        }
      }
    });
    getFocusData();
    getCategoryData();
    getSellingSwiperData();
    getSellingPlistData();
    getBestPlistData();
  }

  void getFocusData() {
    httpsClient.get('api/focus').then((value) {
      var response = value;
      FocusModel focus = FocusModel.fromJson(response.data);
      swiperList.value = focus.result!;
      update();
    });
  }

  void getCategoryData() {
    httpsClient.get("api/bestCate").then((value) {
      var response = value;
      CategoryModel category = CategoryModel.fromJson(response.data);
      CategoryList.value = category.result!;
      update();
    });
  }

  //获取热销臻选里面的轮播图

  getSellingSwiperData() async {
    var response = await httpsClient.get("api/focus?position=2");
    var sellingSwiper = FocusModel.fromJson(response.data);
    bestSellingSwiperList.value = sellingSwiper.result!;
    update();
  }

  //获取热销臻选里面的商品数据
  getSellingPlistData() async {
    var response = await httpsClient.get("api/plist?is_hot=1&pageSize=3");
    var plist = PlistModel.fromJson(response.data);
    sellingPlist.value = plist.result!;
    update();
  }

  //获取热门商品数据
  getBestPlistData() async {
    var response = await httpsClient.get("api/plist?is_best=1");
    var plist = PlistModel.fromJson(response.data);
    bestPlist.value = plist.result!;
    update();
  }

  void showMi() {
    isMi.value = true;
    update();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
